#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re

import bilibili_comment
import identify
import os
from flask import Flask,redirect, request,url_for,render_template,jsonify
import bullet_chatting
import bullet_chatting_analyse
app = Flask(__name__, static_folder=os.path.abspath("./templates/"), static_url_path="")

@app.route('/')
def index():
    return render_template("index.html")

@app.route('/show',methods=['POST'])
def show():
    url = request.form["Ourl"]
    # https://api.bilibili.com/x/v2/reply/main?callback=jQuery33105223745523972867_1664964152096&jsonp=jsonp&next=3&type=1&oid=688653662&mode=3&plat=1&_=1664964152099
    bilibili_comment.main(url)
    # 引入bilibili_comment.py并用获取url对应的评论产生./comments.csv
    result = identify.main()
    # 循环预测list中每条评论的类别并产生如下形式的字典
    # {"positive":[],"negative":[],"neutral":[],"suggestion":[],"question":[]}
    #!使用ajax处理请求直接返回result
    return jsonify(result)

@app.route('/show2',methods=['POST'])
def show2():
    url2 = request.form["Ourl"]
    # url = "https://www.bilibili.com/video/BV1q24y1X7i5/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=037f4f8da80b07954c2b7f1dd39cb5fb"
    bv = re.findall(r"BV[0-9a-zA-Z]+", url2)
    bullet_chatting.main(bv[0])
    result2 = bullet_chatting_analyse.main()
    # 评论：{"positive":[],"negative":[],"neutral":[]}
    # 词云：./edg.png
    return jsonify(result2)

if __name__ == '__main__':

    app.run(debug=True)
